package com.hzjq.core.impl

import com.hzjq.core.bean.CapProgressEntity
import com.hzjq.core.bean.ErrorReadCapEntity
import com.hzjq.core.callback.ProgressCallback
import com.hzjq.core.loader.OnBlastLoader
import com.hzjq.core.work.Works
import com.hzjq.core.worker.BlastQueryWork
import com.hzjq.core.worker.BlastWork
import io.reactivex.functions.Consumer

class BlastImpl : OnBlastLoader {

    private var works: Works? = null

    override fun onBlast(
        maxDelay: Long,
        callback: ProgressCallback<CapProgressEntity>,
        errorCallback: Consumer<ErrorReadCapEntity>
    ) {
        cancel()
        works = Works.Builder.newBuilder()
            .addWork(BlastWork(callback, errorCallback))
            .addWork(BlastQueryWork(maxDelay, callback, errorCallback))
            .build()
            .queue()
    }

    override fun cancel() {
        works?.onDestroy()
    }
}